package com.back.service.impl;

import com.back.excepction.BaseException;
import com.back.mapper.MessagesMapper;
import com.back.mapper.TaskInfoMapper;
import com.back.mapper.UserMapper;
import com.back.model.dto.messages.MessagesDTO;
import com.back.model.dto.taskInfo.TaskInfoQueryDTO;
import com.back.model.entity.Messages;
import com.back.model.entity.TaskInfo;
import com.back.model.entity.User;
import com.back.model.reslut.PageData;
import com.back.model.vo.TaskInfoVO;
import com.back.service.TaskInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
* @author Administrator
* @description 针对表【task_info】的数据库操作Service实现
* @createDate 2025-03-11 15:38:59
*/
@Service
public class TaskInfoServiceImpl extends ServiceImpl<TaskInfoMapper, TaskInfo>
    implements TaskInfoService{

    @Resource
    private MessagesMapper messagesMapper;
    @Resource
    private UserMapper userMapper;


    @Override
    public boolean createTask(TaskInfo taskInfo) {
        //保存任务信息
        List<User> users = userMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getUserType, 2));
        if (users!=null || !users.isEmpty()){
            Random random = new Random();
            int randomNumber = random.nextInt(users.size());
            User user = users.get(randomNumber);
            taskInfo.setUserId(user.getId());
            taskInfo.setRecoverId(user.getId());
            taskInfo.setTaskState(1);
        }
        save(taskInfo);
        return save(taskInfo);
    }

    @Override
    public boolean closeTask(Integer id) {
        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setId(id);
        taskInfo.setTaskState(2);
        return updateById(taskInfo);
    }

    @Override
    public boolean deleteTask(Integer id) {
        return removeById(id);
    }

    @Override
    public PageData<TaskInfoVO> taskList(TaskInfoQueryDTO taskInfoQueryDTO) {

        List<TaskInfoVO> TaskVOS = new ArrayList<>();
        PageData<TaskInfoVO> pageData = new PageData<>();

        Page<TaskInfo> page = new Page<>(taskInfoQueryDTO.getPageNum(), taskInfoQueryDTO.getPageSize());
        IPage<TaskInfo> listPage = baseMapper.selectPage(page,getQueryWrapper(taskInfoQueryDTO));

        listPage.getRecords().forEach(task -> {
            TaskInfoVO TaskVO = new TaskInfoVO();
            BeanUtils.copyProperties(task,TaskVO);
            TaskVOS.add(TaskVO);
        });
        pageData.setListData(TaskVOS);
        pageData.setPageCurrent(listPage.getCurrent());
        pageData.setPageSize(listPage.getSize());
        pageData.setTotal(listPage.getTotal());
        return pageData;
    }

    @Override
    public TaskInfoVO taskInfo(TaskInfoQueryDTO taskInfoQueryDTO) {
        TaskInfo taskInfo = getOne(getQueryWrapper(taskInfoQueryDTO));
        TaskInfoVO taskInfoVO = new TaskInfoVO();
        BeanUtils.copyProperties(taskInfo,taskInfoVO);
        return taskInfoVO;

    }

    /**\
     *
     *发送消息
     * @param taskInfoQueryDTO
     * @return
     */

    @Override
    public TaskInfoVO SendMessage(MessagesDTO messagesDTO) {
        Messages messages = new Messages();
        messages.setSenderId(messagesDTO.getSenderId());
        messages.setContent(messagesDTO.getContent());
        messages.setContentType(messagesDTO.getContentType());
        //todo 发送消息2 创建提示信息表 提示对方有新消息
        //保存信息
        messagesMapper.insert(messages);
        return null;
    }

    private LambdaQueryWrapper<TaskInfo> getQueryWrapper(TaskInfoQueryDTO taskInfoQueryDTO){
        LambdaQueryWrapper<TaskInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotEmpty(taskInfoQueryDTO.getId()),TaskInfo::getId,taskInfoQueryDTO.getId());
        return queryWrapper;
    }

}




